<%--
  @author Kusal Samarakoon
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<script type="text/javascript">

    $(function () {
        var editMode = document.getElementById("editMode").value;
        if (editMode == 2) {
            var fieldIds = new Array('landName', 'alienationMethod');
            enableFields(fieldIds);
            var pls_enter_comment = "<br>" + document.getElementById("pls_enter_comment").value;
            $("#land-association-approve").validate({
                rules: {
                    associationComment: {
                        required: true
                    }
                },
                messages: {
                    associationComment: {
                        required: pls_enter_comment
                    }
                }
            });
            $('#attachFile').hide();
        }
    });

    function viewLandDetails() {
        var id = document.getElementById("landId").value;
        //ajax find land list by parent id
        var url = '${"findClildrenByParentLand.do?landId=' +id + '"}';
        $.get(url, $(this).serialize(), function (data) {
            $("#landDetails").html(data);
            //if list contain only one element display land details
            //else
            //display in tree
        });
    }

    function validateRejection() {
        // $('#rejectComment').show()
        var comment_on_rejection = $('#pls_enter_comment_on_rejection').val();

        $('#associationComment').rules("add", {
            required: true,
            messages: {
                required: comment_on_rejection }
        });
    }

    function  validateFields(){
        var pls_select_item = "<br>" + document.getElementById("pls_select_item").value;
        $('.land-table').dataTable({

        });
        $("#land-association").validate({
            rules:{
                alienationMethod:{
                    selectBox:"default"
                }
            },
            messages:{
                alienationMethod:{
                    selectBox:pls_select_item
                }
            }
        });

        return true;
    }

    /*  $(function() {
     $('#landDetails').hide();
     $('a[rel*=facebox]').facebox(function() {
     $('#landDetails').show();
     });*/
    //  });
</script>

<%--<style type="text/css">
    #landDetails {
        width: 780px;
    }
</style>--%>

<div id="land-registration-module-home-title" class="module-home-title">
    <s:a href="defineAlienationMethodForLand.do">
        <img src="${pageContext.request.contextPath}/images/land-reg.png" alt="Land Registration">
            <span>
                <s:if test="(#session.userGroup=='lo' || #session.userGroup=='orip') &&  editMode !=2">
                    <s:label value="%{getText('define_alienation_methods_for_lands.label')}"/>

                </s:if>
                 <s:else>
                     <s:label value="%{getText('approve_define_alienation_methods_for_lands.label')}"/>
                 </s:else>
            </span>
    </s:a>
    <div id="back-button" align="right">
        <s:form><s:submit value="%{getText('back_btn.label')}" action="defineAlienationMethodForLand"/></s:form>
    </div>
</div>
<div id="init-land-association">
    <s:if test="(#session.userGroup=='lo' || #session.userGroup=='orip') &&  editMode !=2">
        <h2><s:label value="%{getText('define_alienation_methods_for_lands.label')}"/></h2>
        <s:form id="land-association" name="land-association" method="POST" enctype="multipart/form-data"
                onsubmit="javascript:return validateFormForInputDataSyntaxErrors('land-association')">
            <table>
                <tr>
                    <td colspan="2" class="left-align">
                        <div class="action-msg"><s:actionmessage/></div>
                        <div class="action-err"><s:actionerror/></div>
                    </td>
                </tr>
                <tr>
                    <td class="left-align"><s:label value="%{getText('slmsSerialNumber.label')}"/> :</td>
                    <td class="left-align"><a onclick="viewLandDetails();"><s:label name="land.id"/></a></td>
                </tr>
                <tr>
                    <td class="left-align"><s:label value="%{getText('land_name.label')}"/> :</td>
                    <td class="left-align"><s:textfield id="landName" name="land.nameOfTheLand" readonly="true"/></td>
                </tr>

                <tr>
                    <td><s:label value="%{getText('alienation_methods.label')}"/></td>
                    <td><s:checkboxlist id="alienationMethodSetId" list="alienationMethodSet"
                                        name="alienationMethodSelectedList"
                                        value="amSelectedList"
                                        theme="checkbox-theme" disabled="true"/></td>
                        <%--<s:hidden name="newAlienation" value="1"/>--%>
                </tr>

                <tr>
                    <td class="left-align"><s:label value="%{getText('alienation_method.label')}"
                                                    cssClass="mandatory-field"/> :
                    </td>
                        <%--<td><s:select id="alienationMethod" name="alienationMethod" list="alienationMethodList"
                                      headerKey="0"
                                      headerValue="Select"/></td>
    --%>
                    <td><s:select id="alienationMethod" name="alienationMethod"
                                  list="approvedAssociatedAlienationMethodsList"
                                  headerKey="0"
                                  headerValue="%{getText('select.label')}"/></td>
                </tr>
                    <%--<tr>
                        <td class="left-align"><s:label value="Purpose"/> :</td>
                        <td class="left-align"><s:textarea id="purpose" cssClass="width-610-px"
                                                           name="landAssociation.purpose"/></td>
                    </tr>--%>

                <s:if test="landAssociation.defineComment!=null">
                    <tr>
                        <td class="left-align"><s:label value="%{getText('comment.label')}"/></td>
                        <td class="left-align"><s:textarea id="associationComment" name="landAssociation.defineComment"

                                                           cssClass="width-610-px" disabled="true"/></td>
                    </tr>
                </s:if>
                <s:include value="../../documentUpload.jsp"/>

                <s:if test="#session.userGroup=='orip'">
                    <tr>
                        <td colspan="2" class="right-align"><s:submit value="%{getText('save.label')}"
                                                                      action="addLandAssociationForDefineAMORIP" onclick="validateFields()"/></td>
                        <td colspan="2" class="right-align"><s:submit value="%{getText('cancel.label')}"
                                                                      action="defineAlienationMethodForLandORIP"/></td>
                    </tr>
                </s:if>
                <s:else>
                    <tr>
                        <td colspan="2" class="right-align"><s:submit value="%{getText('save.label')}"
                                                                      action="addLandAssociationForDefineAM" onclick="validateFields()"/></td>
                        <td colspan="2" class="right-align"><s:submit value="%{getText('cancel.label')}"
                                                                      action="defineAlienationMethodForLand"/></td>
                    </tr>
                </s:else>


            </table>
            <s:hidden name="land.id"/>
            <s:hidden name="landAssociation.id"/>
            <s:hidden name="landAssociation.version"/>
        </s:form>
    </s:if>
    <s:if test="(#session.userGroup=='ords' || #session.userGroup=='orip') && editMode==2">
        <s:form id="land-association-approve" name="land-association" method="POST"
                onsubmit="javascript:return validateFormForInputDataSyntaxErrors('land-association-approve')">
            <table>
                <tr>
                    <td colspan="2" class="left-align">
                        <div class="action-msg"><s:actionmessage/></div>
                        <div class="action-err"><s:actionerror/></div>
                    </td>
                </tr>
                <tr>
                    <td class="left-align"><s:label value="%{getText('slmsSerialNumber.label')}"/> :</td>
                    <td class="left-align"><a onclick="viewLandDetails();"><s:label name="land.id"/></a></td>
                </tr>
                <tr>
                    <td class="left-align"><s:label value="%{getText('land_name.label')}"/> :</td>
                    <td class="left-align"><s:textfield id="landName"
                                                        name="landAssociation.land.nameOfTheLand"
                                                        readonly="true"/></td>
                </tr>
                <tr>
                    <td><s:label value="%{getText('alienation_methods.label')}"/></td>
                    <td><s:checkboxlist id="alienationMethodSetId" list="alienationMethodSet"
                                        name="alienationMethodSelectedList"
                                        value="amSelectedList"
                                        theme="checkbox-theme" disabled="true"/></td>
                        <%--<s:hidden name="newAlienation" value="1"/>--%>
                </tr>
                <tr>
                    <td class="left-align"><s:label value="%{getText('alienation_methods.label')}"/> :</td>
                    <td><s:select id="alienationMethod" name="alienationMethod"
                                  list="alienationMethodList"
                                  headerKey="0"
                                  headerValue="Select"/></td>
                </tr>
                    <%--<tr>
                        <td class="left-align"><s:label value="Purpose"/> :</td>
                        <td class="left-align"><s:textarea id="purpose" cssClass="width-610-px"
                                                           name="landAssociation.purpose"/></td>
                    </tr>--%>

                    <%-- <tr>
                        <td><s:label value="%{getText('uploaded_documents.label')}"/> :</td>
                    </tr>--%>
            </table>
            <s:include value="../../documentUpload.jsp"/>
            <table>
                <tr>
                    <td><s:label value="%{getText('comment.label')}"/></td>
                    <td colspan="3"><s:textarea id="associationComment" name="landAssociation.defineComment"
                                                rows="1"
                                                cssClass="width-595-px"/></td>
                </tr>

                <s:if test="#session.userGroup=='orip'">
                    <tr>
                        <td colspan="4" class="right-align">
                            <s:submit value="%{getText('approve.label')}" action="approveLandAssociationForDefineAMORIP"
                                      cssClass="cancel"/>
                            <s:submit value="%{getText('reject.label')}" action="rejectLandAssociationForDefineAMORIP"
                                      onclick="javascript:return validateRejection()"/>
                            <s:submit value="%{getText('cancel.label')}" action="defineAlienationMethodForLandORIP"/>
                        </td>
                    </tr>
                </s:if>
                <s:else>
                    <tr>
                        <td colspan="4" class="right-align">
                            <s:submit value="%{getText('approve.label')}" action="approveLandAssociationForDefineAM"
                                      cssClass="cancel"/>
                            <s:submit value="%{getText('reject.label')}" action="rejectLandAssociationForDefineAM"
                                      onclick="javascript:return validateRejection()"/>
                            <s:submit value="%{getText('cancel.label')}" action="defineAlienationMethodForLand"/>
                        </td>
                    </tr>
                </s:else>


            </table>
            <s:hidden name="land.id"/>
            <s:hidden name="landAssociation.id"/>
            <s:hidden name="landAssociation.version"/>
            <s:hidden id="pls_enter_comment" value="%{getText('pls_enter_comment.label')}"/>
        </s:form>
    </s:if>

</div>
<%--
<div id="landDetails">
    <s:include value="landDetails.jsp"/>
    &lt;%&ndash;<s:include value="../../landRegistration/landDetailsReport.jsp"/>&ndash;%&gt;
</div>
--%>
<div id="landDetails">

</div>
<div>
    <s:include value="../../landTree.jsp"/>
</div>

<s:hidden id="pls_select_item" value="%{getText('pls_select_alienation_method.label')}"/>
<s:hidden id="editMode" name="editMode"/>
<s:hidden id="landId" name="land.id"/>
<s:hidden id="pls_enter_comment_on_rejection" value="%{getText('pls_enter_comment_on_rejection.label')}"/>

